package com.payment.web.controller.system;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.ExcelUtil;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.Announcement;
import com.payment.system.service.IAnnouncementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 公告 信息操作处理
 * 
 * @author payment
 * @date 2019-01-14
 */
@Controller
@RequestMapping("/system/announcement")
public class AnnouncementController extends BaseController
{
    private String prefix = "system/announcement";
	
	@Autowired
	private IAnnouncementService announcementService;
	
	@RequiresPermissions("system:announcement:view")
	@GetMapping()
	public String announcement()
	{
	    return prefix + "/announcement";
	}
	
	/**
	 * 查询公告列表
	 */
	@RequiresPermissions("system:announcement:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Announcement announcement)
	{
		startPage();
        List<Announcement> list = announcementService.selectAnnouncementList(announcement);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出公告列表
	 */
	@RequiresPermissions("system:announcement:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Announcement announcement)
    {
    	List<Announcement> list = announcementService.selectAnnouncementList(announcement);
        ExcelUtil<Announcement> util = new ExcelUtil<Announcement>(Announcement.class);
        return util.exportExcel(list, "announcement");
    }
	
	/**
	 * 新增公告
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存公告
	 */
	@RequiresPermissions("system:announcement:add")
	@Log(title = "公告", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Announcement announcement)
	{
		announcement.setUpdateUser(ShiroUtils.getLoginName());
		return toAjax(announcementService.insertAnnouncement(announcement));
	}

	/**
	 * 修改公告
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		Announcement announcement = announcementService.selectAnnouncementById(id);
		mmap.put("announcement", announcement);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存公告
	 */
	@RequiresPermissions("system:announcement:edit")
	@Log(title = "公告", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Announcement announcement)
	{
		announcement.setUpdateUser(ShiroUtils.getLoginName());
		return toAjax(announcementService.updateAnnouncement(announcement));
	}
	
	/**
	 * 删除公告
	 */
	@RequiresPermissions("system:announcement:remove")
	@Log(title = "公告", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(announcementService.deleteAnnouncementByIds(ids));
	}

	/**
	 * 	//修改启用 禁用状态
	 * @param id
	 * @param status
	 * @return
	 */
	@PostMapping("/updateStatusById")
	@ResponseBody
	public AjaxResult updateStatusById(String id, String status)
	{
		return announcementService.updateStatusById(id,status);
	}


	//

	@GetMapping("/announcementAgent")
	@ResponseBody
	public List<Map<String, Object>> roleMenuTreeData(String  announcementId)
	{
		List<Map<String, Object>> tree = announcementService.selectAnnouncementAgent(announcementId);
		return tree;
	}
}
